<?php
/**
 * CService_Controller
 * - Controller for module page Service
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_DAO_DIR . "/ServiceCategory.class.php");
require_once(LIB_CORE_DIR . "/Paging.class.php");

class CService_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuService = true;
    	
    	$this->dataUser = Session::Get("MUSER");
        if (is_array($this->dataUser) && !empty($this->dataUser)) {
        	$this->loginFlag = true;
        }
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Danh sách category';
        $this->tpl_title_list = 'Manager service -> category';
        
    	if (!$this->loginFlag) {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbCSer = new ServiceCategory();
        
        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
	        
	        // Delete by id
	        if ($dataPost['mode'] == 'delete') {
	        	$ojbCSer->delete(array('id' => $dataPost['itemID']));
	        }
	        
	        // Delete all by array id
	    	if ($dataPost['mode'] == 'deleteall') {
	    		$arrPost = $dataPost['checkdel'];
	    		if (count ($arrPost) > 0) {
	    			foreach ($arrPost as $aP) {
	        			$ojbCSer->delete(array('id' => $aP));
	    			}
	    		}
	        }
	        
	        // Up
	        if ($dataPost['mode'] == 'up') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMin = $ojbCSer->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq)));
	        	$idMin = $ojbCSer->getIdBySeq($seqMin);
	        	
	        	$ojbCSer->setAttribute('seq', $seqMin);
	        	$ojbCSer->update(array('id' => $id));
	        	$ojbCSer = new ServiceCategory();
	        	$ojbCSer->setAttribute('seq', $seq);
	        	$ojbCSer->update(array('id' => $idMin));
	        }
	        
	    	// Down
	        if ($dataPost['mode'] == 'down') {
	        	$id = $dataPost['itemID'];
	        	$seq = $dataPost['type'];
	        	$seqMax = $ojbCSer->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq)), true);
	        	$idMax = $ojbCSer->getIdBySeq($seqMax);
	        	
	        	$ojbCSer->setAttribute('seq', $seqMax);
	        	$ojbCSer->update(array('id' => $id));
	        	$ojbCSer = new ServiceCategory();
	        	$ojbCSer->setAttribute('seq', $seq);
	        	$ojbCSer->update(array('id' => $idMax));
	        }
        }
        
        $this->maxseq = $ojbCSer->getMinMax(array(), true);
        $this->minseq = $ojbCSer->getMinMax(array());
        
        // List all
    	$arrParam = array('m' => 'cservice');
    	$arrField = array('id', 'seq', 'tieude', 'tieude_en');
        $condition = array();
		
        $data = $this->paging($arrParam, $arrField, $condition);
        $arrContent = $data["data"];
		$this->arrContent = array();
		if (count ($arrContent) > 0) {
			foreach ($arrContent as $content) {
				$this->arrContent[] = $content;
			}
			$this->currPage = $data["currPage"];
			$this->numberPage = $data["numberPage"];
			$this->strPaging = $data["strPaging"];
		}

        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Sửa tên danh mục';
        $this->tpl_button = "Thêm mới";
        $this->tpl_title_list = 'Manager service -> category add/edit';
        $this->dialog = false;
        
    	if (!$this->loginFlag) {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbCSer = new ServiceCategory();

        if ($this->isPost()) {
        	$this->dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'tieude', 'tieude_en'));
        	$this->id = $this->dataPost['itemID'];
        	
        	if ($this->dataPost['mode'] != '') {
        		// Update database
        		if ($this->dataPost['type'] == 'edit' && $this->id != '') {
        			$ojbCSer->setAttribute('tieude', $this->dataPost['tieude']);
	        		$ojbCSer->setAttribute('tieude_en', $this->dataPost['tieude_en']);
	        		$ojbCSer->update(array('id' => $this->id));
	        		$this->dialog = true;
        		}
        		
        		// Insert Database
        		if ($this->dataPost['type'] == 'edit' && $this->id == '') {
        			$ojbCSer->setAttribute('seq', $ojbCSer->getMinMax(array(), true) + 1);
		        	$ojbCSer->setAttribute('tieude', $this->dataPost['tieude']);
		        	$ojbCSer->setAttribute('tieude_en', $this->dataPost['tieude_en']);
		        	$ojbCSer->setAttribute('insert_at', date('Y-m-d H:i:s'));
		        	$ojbCSer->insert();
		        	$this->dialog = true;
        		}
        	} else {
        		$this->tpl_button = "Sửa";
	        	$this->dataPost = $ojbCSer->getByPrimaryKey($this->id);
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
    
	/**
    * paging
    * - Array $arrParam
    * - Array $condition
    */
	public function paging($arrParam = null, $arrField = array(), $condition = array()) {
		$objPaging = new Paging();
		$ojbCSer = new ServiceCategory();
	    // Get total record of content menu
		$totalRecord = $ojbCSer->getTotalData($condition);
		 // Get page current
        if (isset($_REQUEST["pageNo"])) {
        	$currentPage = $_REQUEST["pageNo"];
        }
        else {
            $currentPage = 1;
        }
		$numberRecord = 15;
        // get page number
		if (!is_null($numberRecord) && $numberRecord != 0) {
			$numberPage = ceil($totalRecord / $numberRecord);
		} else {
			$numberPage = 0;
		}
		// get start record
		$startRecord = ($currentPage - 1) * $numberRecord;

        // get array paging
		$arrPage = $objPaging->getArrPaging($currentPage, $numberPage);
		// get string page
		$strPaging = $objPaging->getStrPaging($arrPage, $currentPage, $numberPage, $arrParam);

        // Get all content menu
        $content = $ojbCSer->getListData($startRecord, $numberRecord, $arrField, $condition);
        $no = $startRecord + 1;
		$result = array("total"=>$totalRecord,
						"data"=>$content,
						"currPage"=>$currentPage,
						"numberPage"=>$numberPage,
						"strPaging"=>$strPaging,
						"no"=>$no);

		return $result;
	}
}
?>

